package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Select extends JInternalFrame {
	
	DefaultTableModel model;
	Vector<String> colName = new Vector<String>();
	DatabaseTest main;
	
	Connection conn;
	private JPanel panel;
	private JTextField sql;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	
	public Select(Connection c, DatabaseTest dt) {
		super("조회", true, true, true, false);
		this.conn = c;
		this.main = dt;
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(0, 300, 450, 300);
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getScrollPane_1(), BorderLayout.CENTER);

	}
	
	public void setList(){
		String s = sql.getText();
		try{
			PreparedStatement pstmt = conn.prepareStatement(s);
			ResultSet rs = pstmt.executeQuery();
			ResultSetMetaData rsm = rs.getMetaData();
			Vector v = new Vector();
			v.add("No");
			
			for(int i=1; i<=rsm.getColumnCount(); i++){
				v.add(rsm.getColumnName(i));
			}
			
			model = new DefaultTableModel(v, 0);
			table.setModel(model);
			int cnt = 1;
			
			while(rs.next()){
				Vector d = new Vector<>();
				d.add(cnt);
				// 컬럼의 카운트에 백터에 저장
				for(int i=1; i<=rsm.getColumnCount(); i++){
					d.add(rs.getString(i));
				}
				model.addRow(d);
				cnt++;
			}
		}catch(Exception ex){
			
		}
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getSql(), BorderLayout.CENTER);
			panel.add(getBtnNewButton(), BorderLayout.EAST);
		}
		return panel;
	}
	private JTextField getSql() {
		if (sql == null) {
			sql = new JTextField();
			sql.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						setList();
					}
				}
			});
			sql.setColumns(10);
		}
		return sql;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("쿼리실행");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setList();
				}
			});
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent ev) {
					if(main.update == null){
						main.openUpdate();
						main.openDelete();
					}
					int row = table.getSelectedRow();
					String s = (String)table.getValueAt(row, 3);
					String c = (String)table.getValueAt(row, 1);
					String e = (String)table.getValueAt(row, 2);
					
					main.update.setDate(s, c, e);
					main.update.setVisible(true);
					
					main.delete.setDate(s, c, e);
					main.delete.setVisible(true);
					
				}
			});
			table.setModel(model = new DefaultTableModel(colName, 0));
		}
		return table;
	}
}
